package gui;

import java.awt.Point;
import java.awt.event.*;
import javax.swing.JPopupMenu;

public abstract class AbstractPopupController extends MouseAdapter {

	public void mousePressed(MouseEvent e) {
		showIfPopupTrigger(e);
	}

	public void mouseReleased(MouseEvent e) {
		showIfPopupTrigger(e);
	}

	public abstract JPopupMenu makePopup(int x, int y);
	
	private void showIfPopupTrigger(MouseEvent mouseEvent) {
		if (mouseEvent.isPopupTrigger()) {
			JPopupMenu popup = makePopup(mouseEvent.getX(), mouseEvent.getY());
			
			if (popup != null) {
				/* Un popup est associé à l'élément sélectionné, on va l'afficher */
				popup.show(mouseEvent.getComponent(), mouseEvent.getX(),
					mouseEvent.getY());
			}
		}
	}
}
